<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>使用 NSOperation 并发编程 | KnightSama‘s Blog</title>
    <meta name="description" content="Hello, my friend!">
    <link rel="icon" href="/favicon.ico">
  <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
  <script src="https://cdn.jsdelivr.net/npm/react/umd/react.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/react-dom/umd/react-dom.production.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/@babel/standalone/babel.min.js"></script>
    
    <link rel="preload" href="/assets/css/0.styles.c4ea52a5.css" as="style"><link rel="preload" href="/assets/js/app.a2483de7.js" as="script"><link rel="preload" href="/assets/js/4.0078dadf.js" as="script"><link rel="preload" href="/assets/js/1.c3e7d2b0.js" as="script"><link rel="preload" href="/assets/js/16.bcee7c14.js" as="script"><link rel="prefetch" href="/assets/js/10.265e4f40.js"><link rel="prefetch" href="/assets/js/11.a3a2f8c1.js"><link rel="prefetch" href="/assets/js/12.1910f8cc.js"><link rel="prefetch" href="/assets/js/13.229fa478.js"><link rel="prefetch" href="/assets/js/14.c27d4a17.js"><link rel="prefetch" href="/assets/js/15.92adafd4.js"><link rel="prefetch" href="/assets/js/17.ee1bcf41.js"><link rel="prefetch" href="/assets/js/18.fa3c3a4e.js"><link rel="prefetch" href="/assets/js/19.1a943c57.js"><link rel="prefetch" href="/assets/js/20.44781dae.js"><link rel="prefetch" href="/assets/js/21.1fe46223.js"><link rel="prefetch" href="/assets/js/22.6f7d46c2.js"><link rel="prefetch" href="/assets/js/23.56223d02.js"><link rel="prefetch" href="/assets/js/24.e7cad43b.js"><link rel="prefetch" href="/assets/js/25.b2902552.js"><link rel="prefetch" href="/assets/js/26.70698c07.js"><link rel="prefetch" href="/assets/js/27.6ee3d263.js"><link rel="prefetch" href="/assets/js/28.ce131de6.js"><link rel="prefetch" href="/assets/js/29.90552487.js"><link rel="prefetch" href="/assets/js/30.b3291ccc.js"><link rel="prefetch" href="/assets/js/31.ea8c441e.js"><link rel="prefetch" href="/assets/js/32.3e29bbeb.js"><link rel="prefetch" href="/assets/js/33.65e78c5e.js"><link rel="prefetch" href="/assets/js/34.3ba19d7c.js"><link rel="prefetch" href="/assets/js/35.66d3fc4a.js"><link rel="prefetch" href="/assets/js/36.f5725281.js"><link rel="prefetch" href="/assets/js/37.f5b0b5ff.js"><link rel="prefetch" href="/assets/js/38.3cae3054.js"><link rel="prefetch" href="/assets/js/39.47dc1ffe.js"><link rel="prefetch" href="/assets/js/5.f0714e2e.js"><link rel="prefetch" href="/assets/js/6.53240a5f.js"><link rel="prefetch" href="/assets/js/7.dc126a6b.js"><link rel="prefetch" href="/assets/js/8.86452400.js"><link rel="prefetch" href="/assets/js/9.16ef06de.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.734d5d92.js">
    <link rel="stylesheet" href="/assets/css/0.styles.c4ea52a5.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-0cb1c278><div data-v-0cb1c278><div id="loader-wrapper" class="loading-wrapper" data-v-4b73742e data-v-0cb1c278 data-v-0cb1c278><div class="loader-main" data-v-4b73742e><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div><div data-v-4b73742e></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-da0660ba data-v-0cb1c278 data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>KnightSama‘s Blog</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div class="hide" data-v-0cb1c278><header class="navbar" data-v-0cb1c278><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/Misaka.jpg" alt="KnightSama‘s Blog" class="logo"> <span class="site-name">KnightSama‘s Blog</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0cb1c278></div> <aside class="sidebar" data-v-0cb1c278><div class="personal-info-wrapper" data-v-4a3def3c><img src="/Misaka.jpg" alt="author-avatar" class="personal-img" data-v-4a3def3c> <h3 class="name" data-v-4a3def3c>
    KnightSama
  </h3> <div class="num" data-v-4a3def3c><div data-v-4a3def3c><h3 data-v-4a3def3c>27</h3> <h6 data-v-4a3def3c>文章</h6></div> <div data-v-4a3def3c><h3 data-v-4a3def3c>14</h3> <h6 data-v-4a3def3c>标签</h6></div></div> <hr data-v-4a3def3c></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/iOS/" class="nav-link"><i class="iconfont undefined"></i>
  iOS
</a></li><li class="dropdown-item"><!----> <a href="/categories/集锦/" class="nav-link"><i class="iconfont undefined"></i>
  集锦
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/Github/" class="nav-link"><i class="iconfont undefined"></i>
  Github
</a></li><li class="dropdown-item"><!----> <a href="/categories/Python/" class="nav-link"><i class="iconfont undefined"></i>
  Python
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><a href="https://github.com/KnightSama" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>使用 NSOperation 并发编程</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Views/20160619.html#创建操作" class="sidebar-link">创建操作</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160619.html#使用-nsinvocationoperation-创建" class="sidebar-link">使用 NSInvocationOperation 创建</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#使用-nsblockoperation-创建" class="sidebar-link">使用 NSBlockOperation 创建</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#变更操作优先级" class="sidebar-link">变更操作优先级</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#操作完成回调" class="sidebar-link">操作完成回调</a></li></ul></li><li><a href="/Views/20160619.html#创建队列" class="sidebar-link">创建队列</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160619.html#主队列" class="sidebar-link">主队列</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#最大并发数" class="sidebar-link">最大并发数</a></li></ul></li><li><a href="/Views/20160619.html#队列的暂停、继续与取消" class="sidebar-link">队列的暂停、继续与取消</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Views/20160619.html#队列暂停" class="sidebar-link">队列暂停</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#队列继续" class="sidebar-link">队列继续</a></li><li class="sidebar-sub-header"><a href="/Views/20160619.html#队列取消" class="sidebar-link">队列取消</a></li></ul></li><li><a href="/Views/20160619.html#操作依赖" class="sidebar-link">操作依赖</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-da0660ba data-v-0cb1c278><h3 class="title" style="display:none;" data-v-da0660ba data-v-da0660ba>使用 NSOperation 并发编程</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-da0660ba data-v-da0660ba><input type="password" value="" data-v-da0660ba> <span data-v-da0660ba>Konck! Knock!</span> <button data-v-da0660ba>OK</button></label> <div class="footer" style="display:none;" data-v-da0660ba data-v-da0660ba><span data-v-da0660ba><i class="iconfont reco-theme" data-v-da0660ba></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-da0660ba>vuePress-theme-reco</a></span> <span data-v-da0660ba><i class="iconfont reco-copyright" data-v-da0660ba></i> <a data-v-da0660ba><span data-v-da0660ba>KnightSama</span>
            
          <!---->
          2021
        </a></span></div></div> <div data-v-0cb1c278><main class="page"><div class="sidebar-open-close"><svg xmlns="http://www.w3.org/2000/svg" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <!----> <div class="page-title" style="display:none;"><h1>使用 NSOperation 并发编程</h1> <hr> <div data-v-73367bb4><i class="iconfont reco-account" data-v-73367bb4><span data-v-73367bb4>KnightSama</span></i> <i class="iconfont reco-date" data-v-73367bb4><span data-v-73367bb4>2016-06-19</span></i> <i class="iconfont reco-eye" data-v-73367bb4><span id="/Views/20160619.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-73367bb4><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-73367bb4><span class="tag-item" data-v-73367bb4>
      iOS
    </span><span class="tag-item" data-v-73367bb4>
      Objective-C
    </span><span class="tag-item" data-v-73367bb4>
      Swift
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><p>在 iOS 中并发编程除了经常使用的 GCD 外还可以使用 NSOperation。与 GCD 相比 NSOperation 是面向对象的，所以更加易于使用，而且 NSOperation 包含了一些 GCD 较难实现的功能，也是苹果推荐的方式。
</p> <p>NSOperation 的主要使用方式就是将操作添加到队列中，而 NSOperation 本身是个抽象类，其只有方法声明并没有实现，所以我们需要使用其子类来创建操作</p> <h2 id="创建操作"><a href="#创建操作" class="header-anchor">#</a> 创建操作</h2> <p>NSOperation 创建的操作默认都是异步执行的，可以通过调用  <code>start</code>  方法在当前线程立即执行操作</p> <h3 id="使用-nsinvocationoperation-创建"><a href="#使用-nsinvocationoperation-创建" class="header-anchor">#</a> 使用 NSInvocationOperation 创建</h3> <p>下面的例子中  <code>demo</code>  是一个普通的方法，NSInvocationOperation 只能在 Objective-C 中使用，Swift 已经移除了这种方法</p> <div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSInvocationOperation <span class="token operator">*</span>operation <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSInvocationOperation alloc<span class="token punctuation">]</span> initWithTarget<span class="token punctuation">:</span><span class="token keyword">self</span> selector<span class="token punctuation">:</span><span class="token keyword">@selector</span><span class="token punctuation">(</span>demo<span class="token punctuation">)</span> object<span class="token punctuation">:</span>nil<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 在当前线程立即执行操作</span>
<span class="token punctuation">[</span>operation start<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="使用-nsblockoperation-创建"><a href="#使用-nsblockoperation-创建" class="header-anchor">#</a> 使用 NSBlockOperation 创建</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSBlockOperation <span class="token operator">*</span>operation <span class="token operator">=</span> <span class="token punctuation">[</span>NSBlockOperation blockOperationWithBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 操作 </span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> operation <span class="token operator">=</span> <span class="token builtin">BlockOperation</span> <span class="token punctuation">{</span>
    <span class="token comment">// 操作 </span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <p>我们可以通过  <code>addExecutionBlock</code>  给 NSBlockOperation 添加多个操作，这多个操作是异步执行的</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token punctuation">[</span>operation addExecutionBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 操作 </span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>operation<span class="token punctuation">.</span>addExecutionBlock <span class="token punctuation">{</span>
    <span class="token comment">// 操作</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h3 id="变更操作优先级"><a href="#变更操作优先级" class="header-anchor">#</a> 变更操作优先级</h3> <p>我们可以通过设置  <code>queuePriority</code>  设置操作的优先级，通过设置  <code>qualityOfService</code>  设置操作的服务质量即系统分配资源的数量</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>operation<span class="token punctuation">.</span>queuePriority <span class="token operator">=</span> NSOperationQueuePriorityHigh<span class="token punctuation">;</span>
operation<span class="token punctuation">.</span>qualityOfService <span class="token operator">=</span> NSOperationQualityOfServiceUserInteractive<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>operation<span class="token punctuation">.</span>queuePriority <span class="token operator">=</span> <span class="token punctuation">.</span>high
operation<span class="token punctuation">.</span>qualityOfService <span class="token operator">=</span> <span class="token punctuation">.</span>userInteractive
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></section></div></div> <h3 id="操作完成回调"><a href="#操作完成回调" class="header-anchor">#</a> 操作完成回调</h3> <p>我们可以给操作设置回调函数，可以在操作执行完毕时执行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token punctuation">[</span>operation setCompletionBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 操作结束时回调</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>operation<span class="token punctuation">.</span>completionBlock <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">// 操作结束时回调</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></section></div></div> <h2 id="创建队列"><a href="#创建队列" class="header-anchor">#</a> 创建队列</h2> <p>我们可以通过 NSOperationQueue 创建队列，将操作放入队列中运行。队列默认是并行的，我们可以通过将<a href="#%E6%9C%80%E5%A4%A7%E5%B9%B6%E5%8F%91%E6%95%B0">最大并发数</a>设置为 1 来让队列变为串行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建队列</span>
NSOperationQueue <span class="token operator">*</span>queue <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSOperationQueue alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 向队列添加一个操作</span>
<span class="token punctuation">[</span>queue addOperation<span class="token punctuation">:</span>operation<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建队列</span>
<span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">OperationQueue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 向队列添加一个操作</span>
queue<span class="token punctuation">.</span><span class="token function">addOperation</span><span class="token punctuation">(</span>operation<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></section></div></div> <p>对于 NSBlockOperation 我们可以通过下面的方式直接向队列添加操作而无需单独创建</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token comment">// 创建队列</span>
NSOperationQueue <span class="token operator">*</span>queue <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">[</span>NSOperationQueue alloc<span class="token punctuation">]</span> init<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// 向队列添加一个操作</span>
<span class="token punctuation">[</span>queue addOperationWithBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
    <span class="token comment">// 操作</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token comment">// 创建队列</span>
<span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token function">OperationQueue</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">// 向队列添加一个操作</span>
queue<span class="token punctuation">.</span>addOperation <span class="token punctuation">{</span>
    <span class="token comment">// 操作</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section></div></div> <h3 id="主队列"><a href="#主队列" class="header-anchor">#</a> 主队列</h3> <p>我们可以通过下面的方式获得主队列，主队列是串行队列</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSOperationQueue <span class="token operator">*</span>queue <span class="token operator">=</span> <span class="token punctuation">[</span>NSOperationQueue mainQueue<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> queue <span class="token operator">=</span> <span class="token builtin">OperationQueue</span><span class="token punctuation">.</span>main
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="最大并发数"><a href="#最大并发数" class="header-anchor">#</a> 最大并发数</h3> <p>我们可以通过设置队列的  <code>maxConcurrentOperationCount</code>  参数来设置最大并发数，当该值设置为 1 时队列将以串行方式运行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>queue<span class="token punctuation">.</span>maxConcurrentOperationCount <span class="token operator">=</span> <span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>queue<span class="token punctuation">.</span>maxConcurrentOperationCount <span class="token operator">=</span> <span class="token number">1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h2 id="队列的暂停、继续与取消"><a href="#队列的暂停、继续与取消" class="header-anchor">#</a> 队列的暂停、继续与取消</h2> <p>我们只能暂停与取消队列中还未执行的操作，正在执行的操作不受影响</p> <h3 id="队列暂停"><a href="#队列暂停" class="header-anchor">#</a> 队列暂停</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>queue<span class="token punctuation">.</span>suspended <span class="token operator">=</span> YES<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>queue<span class="token punctuation">.</span>isSuspended <span class="token operator">=</span> <span class="token boolean">true</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <div class="custom-block warning"><p class="custom-block-title">注意</p> <p>如果先暂停队列，再添加操作到队列，队列不会调度添加的操作。所以在暂停队列之前要判断队列中有没有任务，如果没有任务就不暂停队列。</p></div> <h3 id="队列继续"><a href="#队列继续" class="header-anchor">#</a> 队列继续</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>queue<span class="token punctuation">.</span>suspended <span class="token operator">=</span> NO<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>queue<span class="token punctuation">.</span>isSuspended <span class="token operator">=</span> <span class="token boolean">false</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h3 id="队列取消"><a href="#队列取消" class="header-anchor">#</a> 队列取消</h3> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code><span class="token punctuation">[</span>queue cancelAllOperations<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code>queue<span class="token punctuation">.</span><span class="token function">cancelAllOperations</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></section></div></div> <h2 id="操作依赖"><a href="#操作依赖" class="header-anchor">#</a> 操作依赖</h2> <p>如果一个操作需要另一个操作先完成则可以通过  <code>addDependency</code>  设置依赖。添加依赖需要在添加到队列前执行</p> <div class="tabs-component"><ul role="tablist" class="tabs-component-tabs"></ul> <div class="tabs-component-panels"><section aria-hidden="true" id="first-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-objectivec line-numbers-mode"><pre class="language-objectivec"><code>NSBlockOperation <span class="token operator">*</span>operationA <span class="token operator">=</span> <span class="token punctuation">[</span>NSBlockOperation blockOperationWithBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
NSBlockOperation <span class="token operator">*</span>operationB <span class="token operator">=</span> <span class="token punctuation">[</span>NSBlockOperation blockOperationWithBlock<span class="token punctuation">:</span><span class="token operator">^</span><span class="token punctuation">{</span>
<span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token comment">// A 依赖于 B 先执行</span>
<span class="token punctuation">[</span>operationA addDependency<span class="token punctuation">:</span>operationB<span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section> <section aria-hidden="true" id="second-tab" role="tabpanel" class="tabs-component-panel" style="display:none;"><div class="language-swift line-numbers-mode"><pre class="language-swift"><code><span class="token keyword">let</span> operationA <span class="token operator">=</span> <span class="token builtin">BlockOperation</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
<span class="token keyword">let</span> operationB <span class="token operator">=</span> <span class="token builtin">BlockOperation</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
<span class="token comment">// A 依赖于 B 先执行</span>
operationA<span class="token punctuation">.</span><span class="token function">addDependency</span><span class="token punctuation">(</span>operationB<span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></section></div></div> <div class="custom-block danger"><p class="custom-block-title">注意</p> <p>不能循环建立操作间依赖关系，否则队列不调度操作执行</p></div></div> <footer class="page-edit" style="display:none;"><!----> <!----></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-0cb1c278><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-a81d141e data-v-a81d141e><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-a81d141e><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-a81d141e></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-a81d141e></path></svg></div><canvas id="vuepress-canvas-ribbon"></canvas><div class="my-popup" style="display:none;" data-v-3414bdeb><div class="my-popup-container" data-v-3414bdeb><div class="my-popup-exit" data-v-3414bdeb></div> <img src="" alt data-v-3414bdeb></div></div><div data-v-2ebabc0c></div><div class="kanbanniang" data-v-3b0f9b07><div class="banniang-container" style="display:;" data-v-3b0f9b07><div class="messageBox" style="right:68px;bottom:230px;display:none;" data-v-3b0f9b07>
      欢迎来到 KnightSama‘s Blog
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-3b0f9b07><i class="kbnfont kbn-ban-home ban-home" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-message message" data-v-3b0f9b07></i> <i class="kbnfont kbn-ban-close close" data-v-3b0f9b07></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-3b0f9b07><i class="kbnfont kbn-ban-info info" data-v-3b0f9b07></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-3b0f9b07></i></div> <canvas id="banniang" width="200" height="260" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-3b0f9b07></canvas></div> <div class="showBanNiang" style="display:none;" data-v-3b0f9b07>
    看板娘
  </div></div></div></div>
    <script src="/assets/js/app.a2483de7.js" defer></script><script src="/assets/js/4.0078dadf.js" defer></script><script src="/assets/js/1.c3e7d2b0.js" defer></script><script src="/assets/js/16.bcee7c14.js" defer></script>
  </body>
</html>
